/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.murilojava.compilador.commom;

/**
 * 
 * @author Murilo
 */
public class TabelaIdentificadores {

	public final static char UNDER_LINE = '_';

	private static final char[] TRASH = new char[] { ' ', '\n', '\r' };

	private static final char[] NUMEROS = new char[] { '0', '1', '2', '3', '4',
			'5', '6', '7', '8', '9' };

	private static final char[] PARTE_NUMEROS = new char[] { '.', '-' };

	private static char[] DELIMITADORES = new char[] { ';', ',', '(', ')', '{',
			'}', '[', ']' };

	private static char[] OPERADORES = new char[] { '+', '-', '*', '/', '=',
			'!', '>', '<', '&', '|', '.' };

	private static char[] OPERADORES_SIMPLES = new char[] { '+', '-', '*', '/',
			'>', '<', '=', '.' };

	private static String[] OPERADORES_COMPOSTOS = new String[] { "++", "--",
			"==", "!=", ">=", "<+", "&&" };

	private static char STRING_DELIMITER = '\"';

	private static char CHAR_DELIMITER = '\'';

	private static char COMETARIO_DELIMITER = '/';

	// FIXME ADICIONAR AS OUTRAS
	private static String[] PALAVRAS_RESERVADAS = new String[] { "class",
			"string", "integer", "variables", "methods", "constants", "return",
			"empty", "main", "if", "else", "while", "for", "read", "write",
			"float", "boolean", "true", "false", "extends" };

	public static boolean ehMarcador(char ch) {
		if (verificacaoPadrao(ch, DELIMITADORES))
			return true;

		if (verificacaoPadrao(ch, OPERADORES))
			return true;

		return verificacaoPadrao(ch, TRASH);
	}

	private static boolean verificacaoPadrao(char ch, char[] lista) {
		for (int i = 0; i < lista.length; i++) {
			if (lista[i] == ch)
				return true;
		}

		return false;
	}

	public static boolean ehCaracterValidoIdentificador(char ch) {

		if (Character.isLetter(ch) || Character.isDigit(ch) || ch == UNDER_LINE)
			return true;

		return false;
	}

	public static boolean ehCaracterTrash(char ch) {
		return verificacaoPadrao(ch, TRASH);
	}

	public static boolean ehNumero(char ch) {
		return verificacaoPadrao(ch, NUMEROS);
	}

	public static boolean ehParteNumero(char ch) {
		return verificacaoPadrao(ch, PARTE_NUMEROS);
	}

	public static boolean ehDelimitador(char ch) {
		return verificacaoPadrao(ch, DELIMITADORES);
	}

	public static boolean ehOperador(char ch) {
		return verificacaoPadrao(ch, OPERADORES);
	}

	public static boolean ehOMenos(char ch) {
		if (ch == '-')
			return true;

		return false;
	}

	public static boolean ehBarra(char ch) {
		if (ch == '/')
			return true;
		return false;
	}

	public static boolean ehAsterisco(char ch) {
		if (ch == '*')
			return true;

		return false;
	}

	public static boolean ehOperadoComposto(String opComposto) {
		for (String operador : OPERADORES_COMPOSTOS)
			if (opComposto.equals(operador))
				return true;

		return false;
	}

	public static boolean ehOperadorSimples(char ch) {
		return verificacaoPadrao(ch, OPERADORES_SIMPLES);
	}

	public static boolean ehDelimitadorString(char ch) {
		if (ch == STRING_DELIMITER)
			return true;

		return false;
	}

	public static boolean ehDelimitadorChar(char ch) {
		if (ch == CHAR_DELIMITER)
			return true;

		return false;
	}

	public static boolean ehCharacterStringValido(char ch) {
		if (Character.isLetter(ch))
			return true;
		if (Character.isDigit(ch))
			return true;

		// FIXME Corrigir com os dados do problema.
		int ascii = ((int) ch);
		if ((ascii >= 32 && ascii < 34) || (ascii > 34 && ascii < 39)
				|| (ascii > 39 && ascii <= 126))
			return true;

		return false;
	}

	public static boolean ehPalavraReservada(String identificador) {
		for (String operador : PALAVRAS_RESERVADAS)
			if (identificador.equals(operador))
				return true;

		return false;
	}
}
